home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: const char??
- Date: 27 Feb 1996 18:52:36 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb27115236@qcd.lanl.gov>
- References: <31287436.1235873@news.inforamp.net> <4gb7u8$p5o@sun001.spd.dsccc.com>
- <TANMOY.96Feb20095538@qcd.lanl.gov>
- <4gqflgINN1kq@keats.ugrad.cs.ubc.ca>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: c2a192@ugrad.cs.ubc.ca's message of 25 Feb 1996 12:09:20 -0800
-
- In article <4gqflgINN1kq@keats.ugrad.cs.ubc.ca>
- c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) writes:
- <snip>
- KK: >(There is a related issue that the function may also change *x by
- KK: >using the lvalue *(char*)x. That, and similar tricks, are so bad
- KK: >programming practices, that I won't even comment on them!)
- KK:
- KK: Hmm. Is that not a bit of an error, since the const modifier is
- being dropped
- KK: in the cast? C allows you to only safely convert pointers of any
- type to void *
- KK: and back to the same type. I will have to check references whether
- or not that
- KK: "type" is so restrictive as to include any modifiers like const
- and volatile.
-
- Actually, the C standard guarantees that a pointer to any object type
- (including incomplete object type) can be converted to any other
- object type if one uses a cast. So (char*)x is valid.
-
- It then states that the resultant pointer may not be valid if the
- alignment requirements are not met. As char has the least alignment
- requirement (and in any conversion from a const to a non-const type),
- (char*)x is a valid pointer: so it can be used without invoking
- undefined behaviour. Thus for the expression (char*)x+0, no further
- argumentation would have been necessary.
-
- For *(char*)x, one needs one more step. The standard says that an
- object may be accessed only through an lvalue of a compatible type,
- _or_ an lvalue of a character type. So, use of *(char*)x is valid.
-
- KK:
- KK: In any case, the cast does make it explicit that you are trying to
- voluntarily
- KK: break the const. But in your function declaration you promised
- that you would
- KK: not touch the object---why go through the bother of making that
- promise to the
- KK: caller, when you intend to violate it?
-
- That is precisely what I meant when I said that I do not want to
- discuss it further. By the way, if x is a pointer to the first
- character of a string, *strchr(x,*x) is another way of writing the
- same lvalue as *(char*)x ... but such tricks are really terrible.
-
- KK:
- KK: >An object _defined_ a (i.e. an object whose storage was allocated by a
- KK: >declaration specifying) const, cannot be portably changed by the
- KK: >program. A compiler can assume it's value won't change unless it is
- KK: >also volatile: in which case, even though the program cannot write to
- KK: >it, the compiler has to assume that its value can change due to unkwon
- KK: >reasons.
- KK:
- KK: A read-only device status register would be an example of such a "volatile
- KK: const" beast, no?
-
- Yes, provided the status register is memory mapped :-)
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-